/**
 * router-schema loader
 * @param app koa 实例
 * 通过 json-schema,ajv 对api规则进行约束，配合 api-params-verify 中间件使用
 * app/router-schema/**.js
 * 输出
 *    app.routerSchema = {
 *      api1:jsonSchema,
 *      api2:jsonSchema
 *    }
 */

const { globSync} = require("glob");
const path = require("path")
const { sep } = path

module.exports = (app) => {
    // 读取文件
    const routerSchemaPath = path.resolve(app.businessPath, `.${sep}router-schema`);
    const fileList = globSync(path.resolve(routerSchemaPath, `.${sep}**${sep}**.js`),{
        windowsPathsNoEscape:true
    });

    // 注册所有的routerSchema,可以 ‘app.routerSchema'这样访问
    let routerSchema = {}
    fileList.forEach(file => {
        routerSchema = {
            ...routerSchema,
            ...require(path.resolve(file))
        }
    })
    
    
    app.routerSchema = routerSchema


}